package com.sh.sh_ai_agent.demo.invoke;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONArray;

public class HttpAiInvoke {
    public static void main(String[] args) {
        // 设置你的 DashScope API Key
        String apiKey = TestApiKey.API_KEY; // 推荐从环境变量获取
        if (apiKey == null || apiKey.isEmpty()) {
            throw new RuntimeException("请设置环境变量 DASHSCOPE_API_KEY");
        }

        // 构造请求 JSON
        JSONObject requestBody = new JSONObject();
        requestBody.set("model", "qwen-plus");

        JSONObject input = new JSONObject();
        JSONArray messages = new JSONArray();

        messages.add(new JSONObject()
                .set("role", "system")
                .set("content", "You are a helpful assistant."));

        messages.add(new JSONObject()
                .set("role", "user")
                .set("content", "你是谁？"));

        input.set("messages", messages);
        requestBody.set("input", input);

        JSONObject parameters = new JSONObject();
        parameters.set("result_format", "message");
        requestBody.set("parameters", parameters);

        // 发送请求
        HttpResponse response = HttpRequest.post("https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation")
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .body(requestBody.toString())
                .execute();

        // 输出返回结果
        System.out.println("Response Code: " + response.getStatus());
        System.out.println("Response Body: " + response.body());
    }
}
